﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Logger
{
    /// <summary>
    /// Host Address Message Singleton class that outputs the host name and ip address message with a user message
    /// </summary>
    /// <dates>
    /// 15/05/2011 - created by Chiaki Sato
    /// </dates>
    class HostAddressSingleton : ILoggerMessage
    {
        private const string MESSAGE_SEPARETER = " ";
        private const string MESSAGE_TERMINATOR = " - ";
        private const string MESSAGE_HOST_NAME = "HOST NAME: ";
        private const string MESSAGE_HOST_IP = "IP: ";

        private static string addressMseeage = "";
        private static HostAddressSingleton hostAddressSingleton = new HostAddressSingleton();

        /// <summary>
        /// Defaulta constractor (Singleton)
        /// </summary>
        private HostAddressSingleton()
        {
            string hostname = Dns.GetHostName();
            addressMseeage = MESSAGE_HOST_NAME + hostname + MESSAGE_SEPARETER;

            IPAddress[] addressList = Dns.GetHostAddresses(hostname);
            foreach (IPAddress address in addressList)
            {
                if (address.ToString().Length > 6)
                {
                    // get a decent address
                    addressMseeage = addressMseeage + MESSAGE_HOST_IP + address.ToString() + MESSAGE_TERMINATOR;
                    break;
                }
            }
        }

        /// <summary>
        /// Get singleton instance
        /// </summary>
        public static HostAddressSingleton Instance
        {
            get
            {
                return hostAddressSingleton;
            }
        }

        /// <summary>
        /// Get a host address log message
        /// </summary>
        public string CreateLogMessage()
        {
            return addressMseeage;
        }
    }
}
